package com.app.backend.base.utils;

import cn.hutool.core.util.StrUtil;
import com.app.backend.base.entity.MessageSend;
import com.app.backend.base.entity.User;
import com.app.backend.base.service.UserService;
import com.app.backend.base.vo.EmailMessage;
import com.app.backend.common.constant.ActConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @program: MessageUtil
 * @description:
 * @author: Zhun.Xiao
 * @create: 2020-03-03 12:07
 **/


@Transactional
@Component
@Slf4j
public class MessageUtil {
    @Autowired
    private EmailUtil emailUtil;

    @Autowired
    private MessageSendUtil messageSendUtil;

    @Autowired
    private UserService userService;

    /**
     * 发送消息 支持：邮件/系统消息
     *
     * @param userId
     * @param contentType
     * @param sendMessage
     * @param sendEmail
     */
    @Async
    public void sendActMessage(String userId, String contentType, Boolean sendMessage, Boolean sendEmail) {

        User user = userService.get(userId);
        if (user == null) {
            return;
        }
        MessageSend messageSend = new MessageSend();
        messageSend.setUserId(user.getId());
        if (sendMessage && ActConstant.MESSAGE_TODO_CONTENT.equals(contentType)) {
            // 待办
            messageSend.setMessageId(ActConstant.MESSAGE_TYPE_TODO);
            messageSendUtil.send(messageSend);
        } else if (sendMessage && ActConstant.MESSAGE_PASS_CONTENT.equals(contentType)) {
            // 通过
            messageSend.setMessageId(ActConstant.MESSAGE_TYPE_PASS);
            messageSendUtil.send(messageSend);
        } else if (sendMessage && ActConstant.MESSAGE_REJECT_CONTENT.equals(contentType)) {
            // 驳回
            messageSend.setMessageId(ActConstant.MESSAGE_TYPE_REJECT);
            messageSendUtil.send(messageSend);
        } else if (sendMessage && ActConstant.MESSAGE_DELEGATE_CONTENT.equals(contentType)) {
            // 委托
            messageSend.setMessageId(ActConstant.MESSAGE_TYPE_DELEGATE);
            messageSendUtil.send(messageSend);
        }

        if (StrUtil.isNotBlank(user.getEmail()) && sendEmail) {
            EmailMessage e = new EmailMessage();
            //todo sendEmail 发送邮箱
            log.info("发送邮件");
        }


    }
}